package com.glink.mydemo;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.baize.glinkad.bean.ADInfo;
import com.baize.glinkad.bean.ads.SplashAD;
import com.baize.glinkad.bean.listener.SplashListener;
import com.baize.glinkad.manager.GLinkADManager;
import com.baize.glinkad.utils.ADLogHelper;


public class SplashActivity extends Activity {
    public FrameLayout splashView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        splashView = (FrameLayout) findViewById(R.id.splash_view);
        ADInfo adInfo = new ADInfo();
        adInfo.setAdView(splashView);
        SplashAD splashAD = new SplashAD(SplashActivity.this,"闪屏_Test");
        splashAD.getSplashAD(SplashActivity.this, adInfo, new SplashListener() {
            @Override
            public void onTimeOut() {
                Intent intent = new Intent();
                intent.setClass(SplashActivity.this, MainActivity.class);
                startActivity(intent);
            }

            @Override
            public void onFail(String code, String msg) {
                ADLogHelper.info("SplashAD onFail: " + msg);
                Intent intent = new Intent();
                intent.setClass(SplashActivity.this, MainActivity.class);
                startActivity(intent);
            }

            @Override
            public void onClicked() {
                ADLogHelper.info("SplashAD onClicked");
            }

            @Override
            public void onShow() {
                ADLogHelper.info("SplashAD onShow");
            }

            @Override
            public void onClose() {
                ADLogHelper.info("SplashAD onClose");
                Intent intent = new Intent();
                intent.setClass(SplashActivity.this, MainActivity.class);
                startActivity(intent);
            }

            @Override
            public void onReady() {
                ADLogHelper.info("SplashAD onReady");
            }
        });

    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            // 捕获back键，在展示广告期间按back键，不跳过广告
            if (splashView.getVisibility() == View.VISIBLE) {
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onDestroy() {
        try {
            super.onDestroy();
            GLinkADManager.onDestory(SplashActivity.this);
        } catch (Exception e) {
        }
    }

}
